package com.site.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.BaseController;
import com.entity.Info;
import com.entity.ProductCategory;
import com.service.ContactItemsService;
import com.service.InfoService;
import com.service.ProductCategoryService;
import com.service.ProductionService;

@Controller
@RequestMapping("/")
public class InfoController extends BaseController {
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private ProductionService productionService;
	@Autowired
	private ContactItemsService contactItemsService;
	@Autowired
	private InfoService infoService;
	@RequestMapping("/about")
	public String about(HttpServletRequest request){
		this.initCommon(request, productCategoryService, contactItemsService, productionService);
		Info about = this.infoService.findAbout();
		request.setAttribute("about", about);
		return "site.about";
	}
	@RequestMapping("/contact")
	public String contact(HttpServletRequest request){
		this.initCommon(request, productCategoryService, contactItemsService, productionService);
		Info contact = this.infoService.findContact();
		request.setAttribute("contact", contact);
		return "site.contact";
	}
	@RequestMapping("/news/p{pageNo}")
	public String news(HttpServletRequest request, @PathVariable Integer pageNo){
		this.initCommon(request, productCategoryService, contactItemsService, productionService);
		if(pageNo == null || pageNo < 1){
			pageNo = 1;
		}
		int total = this.infoService.findNewsCount();
		int pageNum = (total-1)/20+1;
		if(pageNo > pageNum){
			pageNo = pageNum;
		}
		List<Info> page = this.infoService.findNewsInPage(20*(pageNo-1), 20);
		request.setAttribute("page", page);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("total", total);
		return "site.news";
	}
	@RequestMapping("/newsinfo/news-{id}")
	public String info(HttpServletRequest request, @PathVariable int id){
		this.initCommon(request, productCategoryService, contactItemsService, productionService);
		Info news = this.infoService.findById(id);
		if(news.getTags() != null){
			String[] tags = news.getTags().split(",");
			if(tags.length > 1){
				List<ProductCategory> newsTags = new ArrayList<ProductCategory>();
				for(int i = 1; i < tags.length; i++){
					newsTags.add(this.productCategoryService.findById(Integer.parseInt(tags[i])));
				}
				request.setAttribute("tags", newsTags);
				List<Info> relateNews = this.infoService.findRelateNews(tags);
				request.setAttribute("relateNews", relateNews);
			}
		}
		request.setAttribute("news", news);
		return "site.newsinfo";
	}
}
